package com.example.tse.rbyair.widget.others;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.tse.rbhttpservice.service.goods.model.GoodsGetSpecs;
import com.example.tse.rbyair.R;
import com.example.tse.rbyair.widget.tagview.Tag;
import com.example.tse.rbyair.widget.tagview.TagListView;
import com.example.tse.rbyair.widget.tagview.TagView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hp on 16/1/26.
 */
public class ProductdetialTagAdapter extends BaseAdapter {


    private Context c;
    private LayoutInflater inflater;
    private List<GoodsGetSpecs> specs = new ArrayList<GoodsGetSpecs>();
    private OnTagClickedListener clickedListener ;
    private String type = "";
    private String productID = "";

    public ProductdetialTagAdapter(Context c,String type){
        this.c = c;
        inflater = LayoutInflater.from(c);
        this.type = type;
    }

    public void setSpecs(List<GoodsGetSpecs> specs){
        this.specs.clear();
        this.specs.addAll(specs);
        notifyDataSetChanged();
    }

    public void setCurrentProductId(String productID){
        this.productID = productID;
    }

    @Override
    public int getCount() {
        return specs.size();
    }

    @Override
    public Object getItem(int position) {
        return specs.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        GoodsGetSpecs ggs = specs.get(position);

        convertView = inflater.inflate(R.layout.productdetialtag_item, null);
        TextView tag_title = (TextView)convertView.findViewById(R.id.tag_title);

        TagListView tagview = (TagListView)convertView.findViewById(R.id.tagview);
        List<Tag> mTags = new ArrayList<Tag>();

        for(int i=0;i<ggs.getSpecValues().size();i++){
            Tag tag = new Tag();
            tag.setIndex(i);
            tag.setId(Integer.parseInt(ggs.getSpecValues().get(i).getProductId()));
            tag.setTitle(ggs.getSpecValues().get(i).getSpecValue());

            if(productID.equals(""+ggs.getSpecValues().get(i).getProductId())){
                tag.setChecked(true);
            }else{
                tag.setChecked(false);
            }
            mTags.add(tag);
        }

        tagview.setTags(mTags);
        tagview.setOnTagClickListener(new TagListView.OnTagClickListener() {
            @Override
            public void onTagClick(TagView tagView, Tag tag) {
                if(clickedListener!=null && !type.equals("2") && !type.equals("4")){
                    clickedListener.onTagClicked(position,tag.getIndex());
                }
            }
        });


//		GridView grid = (GridView)convertView.findViewById(R.id.tag_list);
//		final TagAdapter tagadapter = new TagAdapter(c);
//		grid.setAdapter(tagadapter);
//
        tag_title.setText(ggs.getSpecName());
//
//		List<String> strs = new ArrayList<String>();
//		int max = 0;
//		for(GoodsGetSpecsSpecValues v :ggs.getSpecValues()){
//			strs.add(v.getSpecValue());
//			int len = v.getSpecValue().length();
//			if(len>max)
//				max = len;
//		}
//
//		grid.setColumnWidth(CommonUtils.dip2px(c, max*15+10));
//		grid.setNumColumns(CommonUtils.getScreenWidth(c)/CommonUtils.dip2px(c, max*15+10) -1);
//
//		tagadapter.addData(strs);
//
//		for(int i=0;i<ggs.getSpecValues().size();i++){
//			if(productID.equals(""+ggs.getSpecValues().get(i).getProductId())){
//				tagadapter.selected(i);  //设置默认选中条目
//			}
//		}
//
//		grid.setOnItemClickListener(new OnItemClickListener() {
//
//			@Override
//			public void onItemClick(AdapterView<?> arg0, View arg1, int position0,long arg3) {
//				if(clickedListener!=null && !type.equals("2") && !type.equals("4")){
//					tagadapter.selected(position0);
//					clickedListener.onTagClicked(position,position0);
//
//				}
//			}
//		});

        return convertView;
    }

    public interface OnTagClickedListener{
        void onTagClicked(int position,int position0);
    }

    public void setOnTagClickedListener(OnTagClickedListener clickedListener){
        this.clickedListener = clickedListener;
    }


}
